﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace ToaThuoc_PAK
{
    public partial class LayToaCu : System.Web.UI.Page
    {
        string MaBN = "", doituong = "", strConnect = "";
        private DAL_DonThuoc dal_DonThuoc;
        private DAL_BenhNhan dal_BenhNhan;
        protected void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            MaBN = Request.QueryString["mabn"];//"MCT08000001";//
            lblMaBN.Text = MaBN;
            strConnect = ConfigurationManager.ConnectionStrings["strConnect"].ConnectionString;
            dal_DonThuoc = new DAL_DonThuoc();
            Session["mand"] = "admin";

            BindThongTinBenhNhan();
            if (!IsPostBack)
            {
                danhsachdonthuoc();
            }
        }

        private void danhsachdonthuoc()
        {
            if (MaBN.Trim() == "")
            { return; }
            DataSet ds = dal_DonThuoc.LayDanhSachDonThuoc(MaBN);
            grd.DataSource = ds;
            grd.DataBind();
        }

        private void BindThongTinBenhNhan()
        {
            dal_BenhNhan = new DAL_BenhNhan();
            BenhNhan TTBenhNhanCho = dal_BenhNhan.LayThongTinBenhNhan(MaBN);
            lblHoten.Text = TTBenhNhanCho.HoTen;
            lbltuoi.Text = (DateTime.Now.Year - int.Parse(TTBenhNhanCho.NamSinh)).ToString();
            if (TTBenhNhanCho.GioiTinh == "0")
                lblgioitinh.Text = "Nam";
            else
                lblgioitinh.Text = "Nữ";
            doituong = TTBenhNhanCho.DoiTuong;
            lbldiachi.Text = TTBenhNhanCho.DiaChi;
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.grd.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.grd_ItemCommand);

        }
        #endregion

        private void grd_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            string _maph = e.Item.Cells[1].Text;//.Replace("&nbsp;","");
            string MaNV = Session["mand"].ToString();
            DataSet ds = dal_DonThuoc.LayDonThuocCu(doituong, _maph, MaNV);

            lenhDienThongTin.Text = "\n<script language=javascript>" +
                "\n<!--" +
                "\n var opendoc=window.opener.document; ";
            lenhDienThongTin.Text +=
                "\n for (d=1;d<=opendoc.all('hsodong').value;d++) " +
                "\n { " +
                "\n				" +
                "\n opendoc.all(\"ID\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"tenthuoc\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"soluong\"+d.toString()).value=\"\";" +



                "\n opendoc.all(\"donvi\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"cachdung\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"lan\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"sl\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"dv\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"ghichu\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"dongia\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"hManhomthuoc\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"thuocID\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"txt_dbname\"+d.toString()).value=\"\";" +
                "\n opendoc.all(\"htengoc\"+d.toString()).value=\"\";" +
                "\n	" +
                "\n }";

            lenhDienThongTin.Text += "\n var sdcanthem=" + ds.Tables[0].Rows.Count + "-opendoc.all('hsodong').value;       ";
            lenhDienThongTin.Text += "\n window.opener.ThemDong(sdcanthem); ";
            int i = 0;
            string chuoi = "";
            for (i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                int j = i + 1;
                if (ds.Tables[0].Rows[i]["SoLuongcon"].ToString() != "0")
                {
                    lenhDienThongTin.Text +=
                        "\n if (opendoc.all(\"ID" + j.ToString() + "\"))  opendoc.all(\"ID" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["ID"].ToString() + "\";" +
                        "\n if (opendoc.all(\"ThuocID" + j.ToString() + "\"))  opendoc.all(\"ThuocID" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["ThuocID"].ToString() + "\";" +
                        "\n if (opendoc.all(\"tenthuoc" + j.ToString() + "\")) opendoc.all(\"tenthuoc" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["TenThuoc"].ToString().Replace("\r\n", "\\r\\n") + "\";" +
                        "\n if (opendoc.all(\"soluong" + j.ToString() + "\")) opendoc.all(\"soluong" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["SoLuong"].ToString() + "\";" +


                        "\n if (opendoc.all(\"donvi" + j.ToString() + "\")) opendoc.all(\"donvi" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["DonVi"].ToString().Replace("\r\n", "\\r\\n") + "\";" +
                        "\n if (opendoc.all(\"cachdung" + j.ToString() + "\")) opendoc.all(\"cachdung" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["CachDung"].ToString().Replace("\r\n", "\\r\\n") + "\";" +
                        "\n if (opendoc.all(\"lan" + j.ToString() + "\")) opendoc.all(\"lan" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["SoLan"].ToString() + "\";" +
                        "\n if (opendoc.all(\"sl" + j.ToString() + "\")) opendoc.all(\"sl" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["SoLuongDung"].ToString() + "\";" +
                        "\n if (opendoc.all(\"dv" + j.ToString() + "\")) opendoc.all(\"dv" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["DonViDung"].ToString().Replace("\r\n", "\\r\\n") + "\";" +
                        "\n if (opendoc.all(\"ghichu" + j.ToString() + "\")) opendoc.all(\"ghichu" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["GhiChu"].ToString().Replace("\r\n", "\\r\\n") + "\";" +
                        "\n if (opendoc.all(\"dongia" + j.ToString() + "\")) opendoc.all(\"dongia" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["giathamkhao"].ToString().Replace("\r\n", "\\r\\n") + "\";" +
                        "\n if (opendoc.all(\"hmanhomthuoc" + j.ToString() + "\")) opendoc.all(\"hmanhomthuoc" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["idhotrilieu"].ToString().Replace("\r\n", "\\r\\n") + "\";" +
                        "\n if (opendoc.all(\"thuocID" + j.ToString() + "\")) opendoc.all(\"ThuocID" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["thuocid"].ToString().Replace("\r\n", "\\r\\n") + "\";" +
                        "\n if (opendoc.all(\"txt_dbname" + j.ToString() + "\")) opendoc.all(\"txt_dbname" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["dbname"].ToString().Replace("\r\n", "\\r\\n") + "\";" +
                        "\n if (opendoc.all(\"htengoc" + j.ToString() + "\")) opendoc.all(\"htengoc" + j.ToString() + "\").value=\"" + ds.Tables[0].Rows[i]["thanhphan"].ToString().Replace("\r\n", "\\r\\n") + "\";";
                    if (ds.Tables[0].Rows[i]["dbname"].ToString() == "KhoBH")
                    {
                        lenhDienThongTin.Text += "\n if (opendoc.all(\"tenthuoc" + j.ToString() + "\")) opendoc.all(\"tenthuoc" + j.ToString() + "\").style.color=\"red\";";
                    }
                    else
                    {
                        lenhDienThongTin.Text += "\n if (opendoc.all(\"tenthuoc" + j.ToString() + "\")) opendoc.all(\"tenthuoc" + j.ToString() + "\").style.color=\"black\";";
                    }
                }
                else
                {
                    chuoi += ds.Tables[0].Rows[i]["tenthuoc"].ToString() + "; ";
                }
            }

            //lenhDienThongTin.Text+="\n jsd="+i.ToString();
            if (chuoi != "") lenhDienThongTin.Text += "\n alert('Thuốc: " + chuoi + "không còn trong kho');";
            lenhDienThongTin.Text += "\n window.close();" +
                "\n//-->" +
                "\n</script>";
        }
    }

}